	
<?php
	
	function CountAllSamples($database,$host,$login,$loginpassword){
		$connect = mysql_connect("localhost","createanalysis","createanalysis");	/* opens connection */
		if (!$connect) {
			die('Could not connect: ' . mysql_error());
		}
		mysql_select_db($database, $connect);$result = mysql_query("SELECT COUNT(DISTINCT e.sample_key) AS count FROM sample_details d, sample_entry e WHERE e.sample_key = d.sample_key AND status =  'ONGOING' ");
		$row = mysql_fetch_array($result);
		return $row["count"];
	}
	
	function DisplayAllAnalysis($database,$host,$login,$loginpassword,$page,$stat){
		$connect = mysql_connect("localhost","createanalysis","createanalysis");	/* opens connection */
		if (!$connect) {
			die('Could not connect: ' . mysql_error());
		}
		mysql_select_db($database, $connect);				/* selects database */
		
		$query ="SELECT * FROM sample_details LIMIT ".($page-1) * 30 .",". 30;
		
		$result = mysql_query($query);
				$listall= NULL;
				$inti=0;

			while($row = mysql_fetch_array($result) ){
			
			//CHECK IF DONE HERE
			if($stat!="")
				$query1 = "SELECT COUNT(*) AS count FROM sample_details d, sample_entry e  WHERE e.sample_key=d.sample_key AND e.sample_key = ".$row["sample_key"]." AND status = '$stat'";
			else
				$query1 = "SELECT COUNT(*) AS count FROM sample_details d, sample_entry e  WHERE e.sample_key=d.sample_key AND e.sample_key = ".$row["sample_key"];
			$result1 = mysql_query($query1);
			$row1 = mysql_fetch_array($result1);
			if($row1["count"] > 0){
				
				echo ' <tr> ';
					$query1 = "SELECT casl_no_year, MIN(casl_no_sample) AS min, MAX(casl_no_sample) AS max FROM sample_entry WHERE sample_key = ".$row["sample_key"];
					$result1 = mysql_query($query1);
					echo ' <th>';
					while ($row1 = mysql_fetch_array($result1)){
						printf("<a href='ViewSamples.php?sample_key=%d'/>",$row["sample_key"]);
						echo $row1["casl_no_year"]."-";
						printf("%04d</br>", $row1["min"]);
						echo "upto";
						echo "</br>";
						echo $row1["casl_no_year"]."-";
						printf("%04d</br>", $row1["max"]);
						echo "</br>";
					}
					echo '</th> ';
					
					
					$query1 = "SELECT analysis_required, status FROM sample_entry WHERE sample_key = ".$row["sample_key"];
					$result1 = mysql_query($query1);

					echo ' <th>';
					while ($row1 = mysql_fetch_array($result1)){
						if($row1["status"] == "Ongoing") $status = "ONGOING";
						if($row1["status"] == "Endorsed") $status = "ENDORSED";
						if($row1["status"] == "Finalized") $status = "FINALIZED";
						
						echo $row1["analysis_required"]." ";
						echo "</br>";
					}
					echo '</th> ';
					echo '<th>'.$row["date_created"].'</th>';
					echo '<th>'.$status.'</th>';
					
					echo '<th><input type="button" value="Summary" onClick='."'document.location.href = ".'"ViewSummary.php?sample_key='.$row["sample_key"].'"'."' /></th>";
				echo ' </tr> ';
			}
			
		}
				
		mysql_close($connect);
	}
		
	function DisplayLimitedAnalysis($username,$database,$host,$login,$loginpassword){
		$chars = array('(', ' ', ')');
		$connect = mysql_connect("localhost","createanalysis","createanalysis");	/* opens connection */
		if (!$connect) {
			die('Could not connect: ' . mysql_error());
		}
		mysql_select_db($database, $connect);				/* selects database */
		
		$result = mysql_query("SELECT * FROM sample_entry s, users u WHERE u.username='$username' AND u.name=s.analyst AND s.status != 'Finalized' ");
		//$type = mysql_query("SELECT sub_type FROM sample_entry s, users u WHERE u.USERNAME='$username' AND u.NAME=s.ANALYST");
				$listall= NULL;
				$inti=0;
		while($row = mysql_fetch_array($result) ){
			if ($row['sub_type'] == "Proximate Analysis") {
				echo ' <tr>
				<th><a href="analysis/View'.str_replace($chars, null, $row["analysis_required"]).'.php?year='.$row["casl_no_year"].'&no='.$row["casl_no_sample"].'">'.sprintf("%d-%04d", $row["casl_no_year"], $row["casl_no_sample"]).'</a></th>
				<th>'.$row["analyst"].'</th>
				<th>'.$row["analysis_required"].'</th>
				<th>'.$row["sampling_date"].'</th>
				</tr> ';
			} else {
				echo ' <tr>
				<th><a href="analysis/ViewOtherAnalysis.php?year='.$row["casl_no_year"].'&no='.$row["casl_no_sample"].'&mainType='.$row["main_type"].'&subType='.$row["sub_type"].'&analysisRequired='.$row["analysis_required"].'">'.sprintf("%d-%04d", $row["casl_no_year"], $row["casl_no_sample"]).'</a></th>
				<th>'.$row["analyst"].'</th>
				<th>'.$row["analysis_required"].'</th>
				<th>'.$row["sampling_date"].'</th>
				</tr> ';
			}
		}
				
		mysql_close($connect);
	}
	
	function SearchCASL($casl,$database,$host,$login,$loginpassword){
		$connect = mysql_connect("localhost","createanalysis","createanalysis");	/* opens connection */
		if (!$connect) {
			die('Could not connect: ' . mysql_error());
		}
		mysql_select_db($database, $connect);				/* selects database */
		
		$searchquery = "SELECT * FROM analysis_request where casl_num='$casl'";
		$result = mysql_query($searchquery);
				$listall= NULL;
				$inti=0;
		//$row = mysql_fetch_array($result);
		while($row = mysql_fetch_array($result) ){
			echo ' <tr>
				<th>'. $row["casl_num"].'</th>
				<th>'.$row["name"].'</th>
				<th>'.$row["date_created"].'</th>
				</tr> ';
			}
				
		mysql_close($connect);	
	}
		
	function SearchDate($casl,$database,$host,$login,$loginpassword){
		$connect = mysql_connect("localhost","createanalysis","createanalysis");	/* opens connection */
		if (!$connect) {
			die('Could not connect: ' . mysql_error());
		}
		mysql_select_db($database, $connect);				/* selects database */
		
		$searchquery = "SELECT * FROM analysis_request where casl_num='$casl'";
		$result = mysql_query($searchquery);
				$listall= NULL;
				$inti=0;
		//$row = mysql_fetch_array($result);
		while($row = mysql_fetch_array($result) ){
			echo ' <tr>
				<th>'. $row["casl_num"].'</th>
				<th>'.$row["name"].'</th>
				<th>'.$row["date_created"].'</th>
				</tr> ';
			}
				
		mysql_close($connect);	
	}
	
	function DisplaySamples($sample_key,$database,$host,$login,$loginpassword){
		$chars = array('(', ' ', ')');
		$connect = mysql_connect("localhost","createanalysis","createanalysis");	/* opens connection */
		if (!$connect) {
			die('Could not connect: ' . mysql_error());
		}
		mysql_select_db($database, $connect);				/* selects database */
		
		$result = mysql_query("SELECT * FROM sample_details ");
				$listall= NULL;
				$inti=0;
		//$row = mysql_fetch_array($result);
		$status = "DONE";
		while($row = mysql_fetch_array($result) ){
			echo ' <tr> ';
			printf("<th><a href='ViewSamples.php?sample_key=%s'>%s</a></th> ",$row["sample_key"],$row["company"]);
			echo ' <th>'.$row["name"].'</th> ';
			
				$query1 = "SELECT analysis_required, status FROM sample_entry WHERE sample_key = ".$row["sample_key"];
				$result1 = mysql_query($query1);
				echo ' <th>';
				while ($row1 = mysql_fetch_array($result1)){
					if($row1["status"] == "ONGOING")
						$status = "ONGOING";
					echo $row1["analysis_required"]." ";
					echo ",";
				}
				echo '</th> ';
			echo ' <th>'.$row["date_created"].'</th>';
			echo '<th>'.$status.'</th>';
					
			echo ' </tr> ';
			}
			
			
				
		mysql_close($connect);
	}

?>



